package com.example.controller;

import com.example.common.Result;
import com.example.entity.dto.UserDTO;
import com.example.entity.retail.*;
import com.example.entity.retail.dto.*;
import com.example.mapper.UserMapper;
import com.example.mapper.dao.RetailDao;
import com.example.service.RetailService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/retail")
public class RetailController {
    @Autowired
    private RetailService retailService;

    @Resource
    private RetailDao retailDao;

    @Resource
    private UserMapper userMapper;

    @PostMapping("/add")
    public Result add(@RequestBody PostRetail postRetail)
    {
        retailService.add(postRetail);
        return Result.success();
    }

    @GetMapping("/selectPostAll")
    public Result selectPostAll()
    {
        List<MainPostRetailDTO> list=retailService.selectPostAll(null);
        return Result.success(list);
    }

    @GetMapping("/selectPostByUid")
    public Result selectPostByUid(String uid)
    {
        List<MainPostRetailDTO> list=retailService.selectPostAll(uid);
        return Result.success(list);
    }

    @GetMapping("/selectPostDescByUid")
    public Result selectPostDescByUid(String retailUid)
    {
        System.out.println("我是控制层"+retailUid);
        DescPostRetailDTO descPostRetailDTO=retailService.selectPostDescByUid(retailUid);
        return Result.success(descPostRetailDTO);
    }

    @GetMapping("/selectPostCommentByUid")
    public Result selectPostCommentByUid(String postItem)
    {
        List<CommentPostRetailDTO> list=retailService.selectPostCommentByUid(postItem);
        return Result.success(list);
    }

    @PostMapping("/addComment")
    public Result addComment(@RequestBody CommentRetail commentRetail)
    {
        retailService.addComment(commentRetail);
        return Result.success();
    }

    @GetMapping("/selectPostItemByUid")
    public Result selectPostItemByUid(String retailUid)
    {
        ItemPostRetailDTO item=retailService.selectPostItemByUid(retailUid);
        return Result.success(item);
    }

    @PostMapping("/addNotification")
    public Result addNotification(@RequestBody NotificationRetail notificationRetail)
    {
        String notiUid= UUID.randomUUID().toString().replace("-", "");
        notificationRetail.setNotiUid(notiUid);
        retailDao.addNotification(notificationRetail);
        return Result.success();
    }

    @DeleteMapping("/deleteNotification/{uid}&{notiUid}")
    public Result deleteNotification(@PathVariable String uid,@PathVariable String notiUid)
    {
        System.out.println(uid+notiUid);
        retailDao.deleteNotification(uid,notiUid);
        return Result.success();
    }

    @GetMapping("/selectNotificationByUid")
    public Result selectNotificationByUid(String uid)
    {
        List<NotificationRetail> list=retailDao.selectNotificationByUid(uid);
        for(NotificationRetail notificationRetail : list)
        {
            UserDTO userDTO=userMapper.GetUserInfo(notificationRetail.getUid());
            notificationRetail.setUid(userDTO.getName());
        }
        return Result.success(list);
    }

    @PutMapping("/isRead")
    public Result isRead(String uid,String notiUid)
    {
        retailDao.isRead(uid,notiUid);
        return Result.success();
    }

    @PutMapping("/isAllRead")
    public Result isAllRead(String uid)
    {
        retailDao.isAllRead(uid);
        return Result.success();
    }

    @GetMapping("/isPostLike")
    public Result isPostLike(String uid,String retailUid)
    {
        Boolean isLike=retailDao.isPostLike(uid,retailUid);
        if (isLike==null)
        {
            return Result.success(false);
        }
        return Result.success(isLike);
    }

    @GetMapping("/addLike")
    public Result addLike(String uid,String type,String content)
    {
        System.out.println("我是点赞"+uid+type+content);
        if(type.equals("post")){
            retailDao.addPostLike(uid,content);
        }
        return Result.success();
    }

    @GetMapping("/deleteLike")
    public Result deleteLike(String uid,String type,String content)
    {
        if(type.equals("post")){
            retailDao.deletePostLike(uid,content);
        }
        return Result.success();
    }

    @GetMapping("/selectUserLike")
    public Result selectUserLike(String uid)
    {
        Integer count=0;
        List<MainPostRetailDTO> list=retailService.selectPostAll(uid);
        for (MainPostRetailDTO mainPostRetailDTO : list) {
            count+=mainPostRetailDTO.getRetailLikes();
        }
        return Result.success(count);
    }

    @PostMapping("/follow")
    public Result follow(@RequestBody FollowRetail followRetail,String type)
    {
        String retailUid =followRetail.getToFollow();
        PostRetail postRetail= retailDao.selectPostByUid(retailUid);

        if(type.equals("follow")){
            retailDao.follow(followRetail.getFollowTo(),postRetail.getUid());
        }
        else if(type.equals("disFollow")){
            retailDao.disFollow(followRetail.getFollowTo(),postRetail.getUid());
        }
        else if(type.equals("following"))
        {
            Integer flag=retailDao.isFollow(followRetail.getFollowTo(),postRetail.getUid());
            System.out.println("我是否关注"+flag);
            if(flag==1){

                return Result.success(true);
            }
            else{
                return Result.success(false);
            }
        }
        else if(type.equals("follows"))
        {
             FollowRetailDTO followRetailDTO=retailDao.selectFollows(followRetail.getFollowTo());
            return Result.success(followRetailDTO);
        }
        return Result.success();
    }

}
